import { base_url, handleCode } from "./config";
import { showToast } from "@/commom";
import { getToken } from "@/commom/token";

interface ResponseData<T> {
  code: number;
  message: string;
  result: T;

  data: T;
}
// 根据是开发模式还是生产模式，返回不同的base_url
const BaseUrl = import.meta.env.MODE == "development" ? base_url : "/api";

//  网络请求封装
const request = <T = ResponseData>(
  url: string,
  method: string,
  data: any,
  header: any,
  timeout = 30000,
) => {
  return new Promise<T>((resolve, reject) => {
    uni.showLoading({
      title: "加载中",
    });
    uni.request({
      url: BaseUrl + url,
      method: method as "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS",
      data: data || {},
      header: {
        shop_beauty: getToken() || "", // 从本地存储获取 Token
        "X-Login-Type": "weixin",
        "Content-Type": "application/json;charset=UTF-8",
        // Authorization: "Basic c2FiZXI6c2FiZXJfc2VjcmV0", // 基础认证
        ...header, // 允许用户自定义请求头
      },
      timeout: timeout,
      withCredentials: true,

      success: (res) => {
        const { data, statusCode } = res;
        const resData = data as ResponseData;
        if (statusCode == 200) {
          if (resData.code == 200) {
            // showToast(resData.message);
            resolve(resData as T);
          } else {
            handleCode(resData.code, resData.message);
          }
        } else {
          showToast(resData.message);
          reject(resData);
        }
      },
      fail: (err) => {
        reject(err);
      },
      complete: () => {
        uni.hideLoading();
      },
    });
  });
};

export { request };
